﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CongThongTin.Entities;
using System.Text;

namespace CongThongTin
{
    public partial class TinTuc : System.Web.UI.Page
    {
        CongThongTinEntities _entities;
        int _maLoaiTin;

        protected void Page_Load(object sender, EventArgs e)
        {
            _maLoaiTin = int.Parse(Request.Params["tl"].ToString());
            _entities = new CongThongTinEntities();
            loadTitle();
            loadTinTucTop();
            loadAllTinTuc();
        }
        void loadTitle()
        {
            LOAITIN oLoaiTin = _entities.LOAITINs.FirstOrDefault(lt => lt.MALOAITIN == _maLoaiTin);
            tbTitle.Text="<h2 class='title'>"+oLoaiTin.TENLOAITIN+"</h2>";
        }
        void loadAllTinTuc()
        {
            Entities.LOAITIN oGetLoaiTin = _entities.LOAITINs.SingleOrDefault(l => l.MALOAITIN == _maLoaiTin);
            int maLoai = Convert.ToInt32(oGetLoaiTin.MALOAITIN);

            var vTinTucs = from t in _entities.TINTUCs
                           where t.MALOAITINTUC == maLoai
                           select t;

            StringBuilder html = new StringBuilder();
            int count = 0;
            foreach (var item in vTinTucs)
            {
                string Imageurl = "ImageHandler.ashx?option=2&id=" + item.MATINTUC;
                string tintucUrl = "ChiTiet.aspx?id=" + item.MATINTUC;
                if (count == 0)
                    html.Append(" <ol id=\"singlecomments\" class=\"commentlist\">");

                html.Append("<li class= \"clearfix\">");
                html.Append("<div class=\"user\"><img alt=\"\" src=\"" + Imageurl + "\" class=\"avatar\" /></div>");
                html.Append("<div class=\"message\"> <div class=\"info\">");
                html.Append("<h3><a href=\"" + tintucUrl + "\">" + item.TIEUDE + "</a></h3> </div>");
                html.Append("<br/><br/><p>" + item.TOMTAT + "...</p>");
                html.Append("</div><div class=\"clear\"></div></li> ");

                if (count == vTinTucs.Count())
                    html.Append("</ol>");

                count++;
            }
            string script = @"  $(function () { $('#comments').append('" + html.ToString() + "'); });";
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString() + "RegisterLoadAllTinTuc", script, true);
        }
        void loadTinTucTop()
        {
            Entities.LOAITIN oGetLoaiTin = _entities.LOAITINs.SingleOrDefault(tl => tl.MALOAITIN==_maLoaiTin);
            int maLT = Convert.ToInt32(oGetLoaiTin.MALOAITIN);

            var vTinTuc = _entities.TINTUCs.Where(t => t.MALOAITINTUC == maLT).Take(1);
            Entities.TINTUC oTopTinTuc = new TINTUC();

            foreach (var item in vTinTuc)
            {
                oTopTinTuc.MATHELOAI = item.MATHELOAI;
                oTopTinTuc.LOAITIN = item.LOAITIN;
                oTopTinTuc.MATINTUC = item.MATINTUC;
                oTopTinTuc.TIEUDE = item.TIEUDE;
                oTopTinTuc.TOMTAT = item.TOMTAT;
                oTopTinTuc.HINHMOTA = item.HINHMOTA;
            }

            if (vTinTuc != null)
            {
                string myHTMLTinTucTop = @"
                <img src='ImageHandler.ashx?option=2&id=" + oTopTinTuc.MATINTUC + "' alt='' width='250px' height='150px' /><div class='info'><h4><a href='ChiTiet.aspx?id=" + oTopTinTuc.MATINTUC + "'>" + oTopTinTuc.TIEUDE + "</a></h4></div><p>" + oTopTinTuc.TOMTAT + "</p>";
                tbTopContent.Text = myHTMLTinTucTop;
            }
        }
    }
}